package com.spit.fammo.ui.frg

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.kingja.loadsir.core.LoadService
import com.spit.amoeba.base.BaseFragment
import com.spit.fammo.R
import com.spit.fammo.bean.db.AssetBean
import com.spit.fammo.databinding.FAssetsDetailBinding
import com.spit.fammo.ext.bindViewPager2
import com.spit.fammo.ext.init
import com.spit.fammo.ext.loadServiceInit
import com.spit.fammo.viewmodel.AssetModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * User: Nike
 *  2024/12/19 11:17
 *      资产详情
 */
class AssetsDetailFrg : BaseFragment<AssetModel, FAssetsDetailBinding>() {

    var fragments: ArrayList<Fragment> = arrayListOf()

    lateinit var loadsir: LoadService<Any>

    var type: Int = 0

    override fun initView(savedInstanceState: Bundle?) {
        loadsir = loadServiceInit(mDatabind.includeViewpager.viewPager) {

        }
        arguments?.let {
            type = it.getInt("type", -1)
            val uId = it.getString("id")
            val stocktakeno = it.getString("stocktakeno")
            val statusid = it.getInt("statusid")
            val sBean = it.getString("bean")
            setToolBarTitle(uId)
            mViewModel.viewModelScope.launch(Dispatchers.IO) {
                var assetBean = mViewModel.assetDao.findAssetById(uId, stocktakeno)
                withContext(Dispatchers.Main) {
                    when(type){
                        0 ->{
                            val bean: AssetBean = Gson().fromJson(sBean, AssetBean::class.java)
                            if (assetBean == null) {
                                assetBean = getAssetBean(bean)
                            } else {
//                                assetBean.statusid = bean.statusid
                                assetBean.remarks = bean.remarks.toString()
                                assetBean.imageList = bean.pic
                            }
                        }
                    }

                    var bundle = Bundle()
                    if (statusid != 0){
                        assetBean.statusid = statusid
                    }
                    bundle.putString("bean", Gson().toJson(assetBean))
                    val frg1 = AssetSearchFrg()
                    frg1.arguments = bundle
                    fragments.add(frg1)

                    val frg2 = AssetTextFrg()
                    frg2.arguments = bundle
                    fragments.add(frg2)

                    val frg3 = AssetUploadFrg()
                    frg3.arguments = bundle
                    fragments.add(frg3)


                    mDatabind.includeViewpager.viewPager.init(this@AssetsDetailFrg, fragments)

                    var mTitles =
                        arrayListOf(
                            getString(R.string.search),
                            getString(R.string.details),
                            getString(R.string.remark)
                        )
                    mDatabind.includeViewpager.magicIndicator.bindViewPager2(
                        mDatabind.includeViewpager.viewPager,
                        mTitles
                    )
                    mDatabind.includeViewpager.viewPager.offscreenPageLimit = mTitles.size
                    mDatabind.includeViewpager.viewPager.setCurrentItem(1, false)
                }
            }
        }
    }

    fun getAssetBean(bean: AssetBean): AssetBean {
        var assetBean = AssetBean()
        assetBean.type = bean.type
        assetBean.statusid = bean.statusid
        assetBean.scanStatus = bean.scanStatus
        assetBean.scanTime = bean.scanTime
        assetBean.assetno = bean.assetno
        assetBean.epc = bean.epc
        return assetBean
    }

}